/**
 * This is not referenced from any PAVE file, but reference from the HTML
 */
package jpave.query_interface;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

import assem.DBConn;
import jpave.database.JPaveDBWrapper;
import jpave.dataholders.AssemblyData;
import jpave.dataholders.DatabaseData;
import jpave.dataholders.GlobalSettings;
import jpave.dataholders.SessionData;
import util.Debug.ErrorReport;
import util.methods.BlastArgs;
import util.methods.HostsCfg;
import util.methods.StringHelpers;
import util.methods.UIHelpers;
import util.methods.Version;

public class JPaveApplet extends JApplet 
{
	public void start ()
	{
		if (!Version.checkJavaSupported())
			System.exit(-1);
		System.err.println("Starting viewSingleTCW " + Version.strPAVEver + " " + Version.strRelDate);
		UIHelpers.setIsApplet(true);
		try {
			SessionData session = new SessionData ( );
			
			// Get the assembly parameters from the applet
			DatabaseData db = new DatabaseData ();
			db.setDBName( getParameter("ASSEMBLY_DB") );
			db.setDB_URL( getParameter("DB_URL") );
			db.setQueryUser( getParameter("DB_USER") );
			db.setQueryPassword( getParameter("DB_PASS") );
			db.setAnnotateUser( getParameter("DB_USER") ); 
			db.setAnnotatePassword( getParameter("DB_PASS") ); 
			db.setDBType( DatabaseData.DB_TYPE_MY_SQL );	
			
			new HostsCfg(db.getDB_URL(), db.getQueryUser(), db.getQueryPassword());
			
			BlastArgs.evalBlastPath("", "", ""); // 2nd arg is diamond, which is not used for interactive blast
			if (!BlastArgs.foundABlast())
			{
				BlastArgs.evalBlastPath("/usr/local/ncbi/blast/bin", "", "");
			}
			
			AssemblyData assembly = new AssemblyData ();
			assembly.setDBData( db );	
			if (!DBConn.checkMysqlServer(db.getDB_URL(), db.getQueryUser(), db.getQueryPassword()))
			{
				JOptionPane.showMessageDialog(null, "Unable to connect to MySQL database.");
				System.exit(0);
			}
	        try {
	    	        Connection conn = db.createDBConnection();
	    	        JPaveDBWrapper.getSetAssemblyParams( conn, assembly);	    	        
	    	        conn.close();
	        }
	        catch (Exception e) { 
	        		ErrorReport.reportError(e, "Getting connection");
	        }
	        
			session.addAssembly( assembly );
			
			// Start page URL
			if ( !StringHelpers.isEmpty ( getParameter("START_PAGE_URL") ) )
				session.setStartURL ( getParameter("START_PAGE_URL") );
			
			theSettings = new GlobalSettings ();
			theSettings.setSelectedSession( session );
			theSettings.setApplet( this );
				
			// See if the applet is running in an debugger
			String str = getAppletContext().getClass().toString();
			theSettings.setAppletDebug( str.equals( "class sun.applet.AppletViewer" ) );
			
			openAssembly ( );
			
		}
		catch ( Exception err )
		{
			err.printStackTrace();
			ErrorReport.reportError(err, "Starting applet");
			theSettings = null;
		}
	}
	
	public void openAssembly ( )
	{
		if ( theFrame != null )
			theFrame.toFront();
		else if ( theSettings != null )
		{
			System.err.println("Opening assembly '" + 
					theSettings.getSelectedSession().getAssembly(0).getAssemblyID() +
					"' ..."); 
			theFrame = new JPaveFrame ( theSettings );
			theFrame.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					theFrame = null;
				}

				public void windowClosed(WindowEvent e) {
					theFrame = null;
				}
			});	
		}
	}
	
	public void stop() 
	{
		System.err.println("Closing assembly '" + 
				theSettings.getSelectedSession().getAssembly(0).getAssemblyID() +
				"' ...");
		theFrame = null;
	}
	
	private JPaveFrame theFrame = null;
	private GlobalSettings theSettings = null;
	
    private static final long serialVersionUID = 1;
}
